package com.zbw.controller;

import com.zbw.domain.Admin;
import com.zbw.domain.BookCategory;
import com.zbw.domain.User;
import com.zbw.domain.Vo.BookVo;
import com.zbw.service.IAdminService;
import com.zbw.service.IBookCategoryService;
import com.zbw.service.IUserService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@Api(value = "AdminController", tags = { "管理员相关接口" })
public class AdminController {
//123
    @Resource
    private IAdminService adminService;
    @Resource
    private IBookCategoryService bookCategoryService;
    @Resource
    private IUserService userService;

    /**
     * 判断admin是否存在
     *
     * @param adminName
     * @return
     */
    @ApiOperation(value = "判断admin是否存在",notes="方法的备注说明")
    @ApiImplicitParam(name = "adminName", value = "用户名", required = true, paramType = "query", dataType = "String")

    @GetMapping ("/isAdminExist")
    @ResponseBody
    public Map adminIsExist(@Param("adminName") String adminName) {
        boolean b = adminService.adminIsExist(adminName);
        Map<String,Object> map=new HashMap();
        if (b) {
            map.put("code",200);
            map.put("msg","该账号存在");
            map.put("content","~~~~");
            return map;
        } else {
            map.put("code",400);
            map.put("msg","该账号不存在");
            map.put("content","nnnnnn");
            return map;
        }
    }

    /**
     * 管理员登陆
     *
     * @param userName
     * @param password
     * @return
     */
    @ApiOperation(value = "管理员登陆")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userName", value = "用户名", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "password", value = "密码", required = true, paramType = "query", dataType = "String")
    })

    @PostMapping("/adminLogin")
    public String adminLogin(
            @Param("userName") String userName,
            @Param("password") String password,
            HttpServletRequest request) {
        Admin admin = adminService.adminLogin(userName, password);

        if (admin == null) {
            // flag 为 1 表示 登录失败 
            request.getSession().setAttribute("flag", 1);
            //return "index";
        }

        // flag = 0 表示用户名密码校验成功
        request.getSession().setAttribute("flag", 0);
        request.getSession().setAttribute("admin", admin);
        return "admin/index";
    }

    /**
     * 返回添加书籍页面
     */
//    @ApiOperation(value="",hidden = true)
    @ApiIgnore
    @RequestMapping("/addBookPage")
    public String addBookPage() {
        return "admin/addBook";
    }

    /**
     * 返回添加类别页面
     */
    @RequestMapping("/addCategoryPage")
    @ApiIgnore
    public String addCategoryPage(@RequestParam("pageNum") int pageNum, Model model) {
        List<BookCategory> page = bookCategoryService.selectBookCategoryByPageNum(pageNum);
        model.addAttribute("page", page);
        return "admin/addCategory";
    }

    /**
     * 返回查询状态页面
     */
    @RequestMapping("/showStausPage")
    @ApiIgnore
    public String showStatusPage() {
        return "admin/showStaus";
    }

    /**
     * 返回管理员首页
     */
    @RequestMapping("/adminIndex")
    @ApiIgnore
    public String returnAdminIndexPage() {
        return "admin/index";
    }


    /**
     * 返回查询用户页面
     */
    @RequestMapping("/showUsersPage")
    @ApiIgnore
    public String showUsersPage(Model model, @RequestParam("pageNum") int pageNum) {
        Page<User> page = userService.findUserByPage(pageNum);
        model.addAttribute("page", page);
        return "admin/showUsers";
    }

    /**
     * 返回&emsp;&emsp;查询书籍页面
     */
    @RequestMapping("/showBooksPage")
    @ApiIgnore
    public String showBooksPage(Model model) {
        Page<BookVo> page = new Page<BookVo>();
//        page.setPageCount(1);
//        page.setPageNum(1);
        model.addAttribute("page", page);
        return "admin/showBooks";
    }


    /**
     * 管理员退出登陆
     *
     * @param request
     * @return
     */
    @PostMapping("/adminLogOut")
    @ApiOperation("管理员登出")
    public String userLogOut(HttpServletRequest request) {
        request.getSession().invalidate();
        return "index";
    }

    /**
     * 返回新增用户页面
     */
    @RequestMapping("/addUserPage")
    @ApiIgnore
    public String addUserPage() {
        return "admin/addUser";
    }

    @RequestMapping("/adminInfoPage")
    @ApiIgnore
    public String adminInfo() {
        return "admin/adminInfo";
    }
    
    /**
     * 更新管理员信息
     *
     * @param admin
     * @param request
     * @return
     */
    @RequestMapping(value="/updateAdmin",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("admin更新接口")
    public boolean updateAdmin(Admin admin, HttpServletRequest request) {
        return adminService.updateAdmin(admin, request);
    }

}
